package com.elai.overview.controller;

import java.util.ArrayList;
import java.util.List;

import com.elai.common.core.domain.model.LoginUser;
import com.elai.common.utils.SecurityUtils;
import com.elai.common.utils.StringUtils;
import com.elai.overview.domain.MyOverviewPage;
import com.elai.overview.domain.MyOverviewPageMarkRela;
import com.elai.overview.service.IMyOverviewPageMarkRelaService;
import com.elai.overview.service.IMyOverviewPageService;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.enums.BusinessType;
import com.elai.overview.domain.MyOverviewBookMark;
import com.elai.overview.service.IMyOverviewBookMarkService;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.common.core.page.TableDataInfo;

/**
 * 我的书签Controller
 * 
 * @author elai
 * @date 2021-09-19
 */
@RestController
@RequestMapping("/overview/mark")
public class MyOverviewBookMarkController extends BaseController
{
    @Autowired
    private IMyOverviewBookMarkService myOverviewBookMarkService;

    @Autowired
    private IMyOverviewPageMarkRelaService myOverviewPageMarkRelaService;

    @Autowired
    private IMyOverviewPageService myOverviewPageService;
    /**
     * 查询我的书签列表
     */
    @PreAuthorize("@ss.hasPermi('overview:mark:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyOverviewBookMark myOverviewBookMark)
    {
        LoginUser user= SecurityUtils.getLoginUser();
        myOverviewBookMark.setUserId(user.getUser().getUserId());
        startPage();
        List<MyOverviewBookMark> list = myOverviewBookMarkService.selectMyOverviewBookMarkList(myOverviewBookMark);
        MyOverviewPage myOverviewPage=new MyOverviewPage();
        myOverviewPage.setUserId(user.getUser().getUserId());
        List<MyOverviewPage> pageList = myOverviewPageService.selectMyOverviewPageList(myOverviewPage);

        list.stream().forEach(mark->{
            MyOverviewPageMarkRela param=new MyOverviewPageMarkRela();
            param.setMarkId(mark.getId());
            List<MyOverviewPageMarkRela> relaList= myOverviewPageMarkRelaService.selectMyOverviewPageMarkRelaList(param);
            List<MyOverviewPageMarkRela> myRelaList=new ArrayList<>();
            pageList.forEach(page->{
                MyOverviewPageMarkRela rela=new MyOverviewPageMarkRela();
                rela.setMarkId(mark.getId());
                rela.setPageId(page.getId());
                rela.setPageName(page.getOverviewName());
                MyOverviewPageMarkRela markRela=relaList.stream().filter(p -> p.getPageId() == page.getId()).findAny().orElse(null);
                if(markRela!=null){
                    rela.setCheckFlag("Y");
                }
                else{
                    rela.setCheckFlag("N");
                }
                myRelaList.add(rela);
            });
            mark.setPageRelaList(myRelaList);
        });
        return getDataTable(list);
    }

    /**
     * 导出我的书签列表
     */
    @PreAuthorize("@ss.hasPermi('overview:mark:export')")
    @Log(title = "我的书签", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyOverviewBookMark myOverviewBookMark)
    {
        LoginUser user= SecurityUtils.getLoginUser();
        myOverviewBookMark.setUserId(user.getUser().getUserId());
        List<MyOverviewBookMark> list = myOverviewBookMarkService.selectMyOverviewBookMarkList(myOverviewBookMark);
        ExcelUtil<MyOverviewBookMark> util = new ExcelUtil<MyOverviewBookMark>(MyOverviewBookMark.class);
        return util.exportExcel(list, "我的书签数据");
    }

    /**
     * 获取我的书签详细信息
     */
    @PreAuthorize("@ss.hasPermi('overview:mark:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(myOverviewBookMarkService.selectMyOverviewBookMarkById(id));
    }

    /**
     * 新增我的书签
     */
    @PreAuthorize("@ss.hasPermi('overview:mark:add')")
    @Log(title = "我的书签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyOverviewBookMark myOverviewBookMark)
    {
        LoginUser user= SecurityUtils.getLoginUser();
        myOverviewBookMark.setUserId(user.getUser().getUserId());
        myOverviewBookMark.setCreateBy(user.getUsername());
        return toAjax(myOverviewBookMarkService.insertMyOverviewBookMark(myOverviewBookMark));
    }

    @GetMapping(value = "/setMakrToOverview")
    public AjaxResult setMakrToOverview(@Param("id") Long id,@Param("pageIds") String pageIds)
    {
        if(StringUtils.isEmpty(pageIds)){
            return AjaxResult.error("pageIds不能为空！");
        }
        myOverviewPageMarkRelaService.deleteMyOverviewPageMarkRelaById(id);
        String pagesId[]=pageIds.split(",");
        for(String pid:pagesId){
            MyOverviewPageMarkRela rela=new MyOverviewPageMarkRela();
            rela.setMarkId(id);
            rela.setPageId(Long.valueOf(pid));
            myOverviewPageMarkRelaService.insertMyOverviewPageMarkRela(rela);
        }
        return AjaxResult.success(myOverviewBookMarkService.selectMyOverviewBookMarkById(id));
    }

    /**
     * 修改我的书签
     */
    @PreAuthorize("@ss.hasPermi('overview:mark:edit')")
    @Log(title = "我的书签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyOverviewBookMark myOverviewBookMark)
    {
        return toAjax(myOverviewBookMarkService.updateMyOverviewBookMark(myOverviewBookMark));
    }

    /**
     * 删除我的书签
     */
    @PreAuthorize("@ss.hasPermi('overview:mark:remove')")
    @Log(title = "我的书签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(myOverviewBookMarkService.deleteMyOverviewBookMarkByIds(ids));
    }
}
